﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.weixin.Response
{
    public partial class KeywordRuleList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _framePage = new FrameHelper(this);

            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                _framePage.RecordCount = Convert.ToInt32(session.CreateQuery("select count(*) from WxResponseRule where RequestType='Keyword' and Kind = 'KEYWORD' and OwnerAccount = '" + _userAccount + "' ").UniqueResult());

                repeater1.DataSource = session.CreateQuery("from WxResponseRule where OwnerAccount = '" + _userAccount + "' and RequestType='Keyword' and Kind = 'KEYWORD'")
                    .SetMaxResults(pageSize)
                    .SetFirstResult((pageIndex - 1) * pageSize)
                    .List<WxResponseRule>();
            }
            repeater1.DataBind();
        }

        public string GetMaterial(object item)
        {
            string result = "";
            WxResponseRule rule = item as WxResponseRule;
            
            using(ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                WxMaterial material = session.CreateCriteria<WxMaterial>().Add(Expression.Eq("Id", rule.ResponseMaterialId)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxMaterial>();
                if (material != null)
                {
                    result = material.Title;
                }
            }
            return result;
        }
    }
}